// pages/shop/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filter: [
      {},
      {},
      {
        title: '默认排序', is_show: false, list: [
          { value: '默认排序', is_selected: true },
          { value: '距离排序', is_selected: false },
          { value: '均价排序', is_selected: false },
          { value: '热度排序', is_selected: false },
        ]
      }
    ],
    shop_list: [],
    cates: [],
    cid: "",
    aid: ""
  },
  backto: function (e) {
    wx.navigateBack({})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中…',
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/adv?type=shop&token=' + this.data.token,
      success: res => {
        this.setData({
          advList: res.data
        })
      }
    })
    // wx.request({
    //   url: 'https://www.58zltc.com/api.php/Index/cate?token=' + this.data.token,
    //   success: res => {
    //     this.setData({
    //       cate: res.data
    //     })
    //   }
    // })
    //请求后台获取分类
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/getCate?type=shop&id=' + options.id + '&token=' + this.data.token,
      success: res => {
        console.log(res);
        var new_filter = this.data.filter;
        new_filter[0] = res.data;
        this.setData({
          filter: new_filter
        })
      }
    })
    //请求后台获取地区
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/getAddressFilter?token=' + this.data.token,
      success: res => {
        var new_filter = this.data.filter;
        new_filter[1] = res.data;
        this.setData({
          filter: new_filter
        })
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/cate?type=shop&id='+ options.id +'&token=' + this.data.token,
      success: res => {
        const cates = res.data;
        var news = [];
        var item = [];
        cates.forEach(function (cate, index) {
          item.push(cate);
          console.log(item.length);
          if (index + 1 === cates.length) {
            news.push(item);
          }
          if (item.length == 10) {
            news.push(item);
            item = [];
          }
        })
        this.setData({
          cates: news
        })
        wx.hideLoading()
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Shop?cid=' + options.id +'&token=' + this.data.token,
      success: res => {
        this.setData({
          shop_list: res.data
        })
        wx.hideLoading()
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Shop/topic?cid=' + options.id + '&token=' + this.data.token,
      success: res => {
        this.setData({
          topic_list: res.data
        })
        wx.hideLoading()
      }
    })
  },
  setFilterPanel: function (e) {
    var findex = e.currentTarget.dataset.findex;
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      if (key == findex) {
        filter[key].is_show = !value.is_show;
      } else {
        filter[key].is_show = false;
      }
    })
    this.setData({
      filter: filter
    })
  },
  hideFilter: function () {
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      filter[key].is_show = false;
    })
    this.setData({
      filter: filter
    })
  },
  setCateIndex: function (e) {
    var cid = e.currentTarget.dataset.cid;
    this.setData({
      cid: cid
    })
    var keys = e.currentTarget.dataset.keys;
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      if (value.is_show) {
        value.list.forEach(function (vo, idx) {
          if (idx == keys) {
            filter[key].list[idx].is_selected = true;
          } else {
            filter[key].list[idx].is_selected = false;
          }
        })
      }
    })
    this.setData({
      filter: filter
    })
    //隐藏分类选项
    if(!this.data.filter[1].is_show){
      //点击更新店铺
      this.getShopByCate();
      this.hideFilter();
    }
    if (this.data.filter[1].list[0].is_selected) {
      //点击更新店铺
      this.getShopByCate();
      this.hideFilter();
    }

  },
  setSubcateIndex: function (e) {
    var aid = e.currentTarget.dataset.aid;
    this.setData({
      aid: aid
    })
    var subindex = e.currentTarget.dataset.key;
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      if (value.is_show) {
        value.list.forEach(function (item, index) {
          if (item.is_selected) {
            item.sub_list.forEach(function (vo, idx) {
              if (idx == subindex) {
                filter[key].list[index].sub_list[idx].is_selected = true;
              } else {
                filter[key].list[index].sub_list[idx].is_selected = false;
              }
            })
          }
        })
      }
    })
    this.setData({
      filter: filter
    })

    //点击更新店铺
    this.getShopByCate();
    //隐藏分类选项
    this.hideFilter();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
/**
 * 用户点击分类 地区 排序等检索条件
 */
  getShopByCate: function () {
    var t = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Shop?cid=' + this.data.cid + '&aid=' + this.data.aid +'&token=' + this.data.token,
      success: function(res) {
        //处理返回数据
        t.setData({
          shop_list: res.data
        }) 
         //请求数据后延时关闭加载中
        if(res.data.length > 0){
          setTimeout(function () {
            wx.hideLoading()
          }, 500)
        }else{
          wx.hideLoading()
        }
      },
      fail:function(){
        wx.hideLoading()
      }
    })
  }

})